package  com.ruoyi.teaching.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.li.work.mapper.SysUserMapper;
import  com.ruoyi.teaching.domain.PlatformMMap;
import com.ruoyi.teaching.domain.PlatformMMapType;
import com.ruoyi.teaching.domain.UniversalCreateUpdate;
import com.ruoyi.teaching.domain.vo.PlaceVO;
import com.ruoyi.teaching.domain.vo.PlatformMMapVO;
import  com.ruoyi.teaching.mapper.PlatformMMapMapper;
import com.ruoyi.teaching.mapper.PlatformMMapTypeMapper;
import  com.ruoyi.teaching.service.PlatformMMapService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.teaching.utils.UniversalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王旭
 * @since 2024-10-12
 */
@Service
public class PlatformMMapServiceImpl extends ServiceImpl<PlatformMMapMapper, PlatformMMap> implements PlatformMMapService {

    @Autowired
    private PlatformMMapMapper platformMMapMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private PlatformMMapTypeMapper platformMMapTypeMapper;

    @Override
    public AjaxResult panoramaMapList(Integer pageNum, Integer pageSize, String message, Long mapType) {
        Page<PlatformMMap> platformMMapPage = new Page<>(pageNum, pageSize);
        QueryWrapper<PlatformMMap> platformMMapQueryWrapper = new QueryWrapper<>();
        if(!"".equals(message) && null != message){
            platformMMapQueryWrapper.like("map_name", message);
        }
        if (null != mapType && mapType != 0){
            platformMMapQueryWrapper.eq("map_type", mapType);
        }
        platformMMapQueryWrapper.orderByDesc("create_time");
        Page<PlatformMMap> platformMMapPage1 = platformMMapMapper.selectPage(platformMMapPage, platformMMapQueryWrapper);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",platformMMapPage1);
    }

    @Override
    public AjaxResult panoramaMapAndPoints(Long id) {
        PlatformMMap platformMMap = platformMMapMapper.selectById(id);
        if(null == platformMMap){
            return new AjaxResult(HttpStatus.ERROR,"查询失败，该地图不存在");
        }
        PlatformMMapVO platformMMapVO = new PlatformMMapVO();
        BeanUtils.copyProperties(platformMMap, platformMMapVO);
        PlaceVO placeVO = JSON.parseObject(platformMMap.getAllMap(), PlaceVO.class);
        platformMMapVO.setAllMap(placeVO);
        if(null != platformMMap.getMapType()){
            PlatformMMapType platformMMapType = platformMMapTypeMapper.selectById(platformMMap.getMapType());
            platformMMapVO.setMapTypeName(platformMMapType.getMapTypeTitle());
        }
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",platformMMapVO);
    }

    @Override
    public AjaxResult addMapOrPoints(PlatformMMap platformMMap) {
        UniversalCreateUpdate create = UniversalUtil.getCreateUpdate(sysUserMapper);
        PlaceVO placeVO = JSON.parseObject(platformMMap.getAllMap(), PlaceVO.class);
        if(placeVO.getMapType() != null){
            platformMMap.setMapType(placeVO.getMapType());
        }else {
            return new AjaxResult(HttpStatus.ERROR, "添加失败，请选择所属分类");
        }
        if (StringUtils.isBlank(placeVO.getPlaceName())) {
            return new AjaxResult(HttpStatus.ERROR, "添加失败，地图名称不能为空");
        }

        String placeName = placeVO.getPlaceName();
        if (placeName.length() > 15) {
            return new AjaxResult(HttpStatus.ERROR, "添加失败，名称不能超过15个字符");
        }

        platformMMap.setMapName(placeVO.getPlaceName());
        platformMMap.setMapUrl(placeVO.getPlaceImg());
        platformMMap.setMapDetail(placeVO.getMapDetail());
        if(StringUtils.isNotEmpty(placeVO.getCreateBy())){
            platformMMap.setCreateBy(placeVO.getCreateBy());
        }else {
            platformMMap.setCreateBy(create.getName());
            platformMMap.setCreateId(create.getId());
        }
        platformMMap.setCreateBy(create.getName());
        platformMMap.setCreateTime(DateUtils.getNowDate());
        int insert = platformMMapMapper.insert(platformMMap);
        if (insert > 0) {
            return new AjaxResult(HttpStatus.SUCCESS, "添加成功");
        } else {
            return new AjaxResult(HttpStatus.ERROR, "添加失败");
        }
    }

    @Override
    public AjaxResult updateMapOrPoints(PlatformMMap platformMMap) {
        UniversalCreateUpdate update = UniversalUtil.getCreateUpdate(sysUserMapper);
        PlaceVO placeVO = JSON.parseObject(platformMMap.getAllMap(), PlaceVO.class);
        platformMMap.setMapName(placeVO.getPlaceName());
        platformMMap.setUpdateBy(update.getName());
        platformMMap.setMapType(placeVO.getMapType());
        platformMMap.setUpdateTime(DateUtils.getNowDate());
        platformMMap.setUpdateId(update.getId());
        int i = platformMMapMapper.updateById(platformMMap);
        if (i > 0) {
            return new AjaxResult(HttpStatus.SUCCESS, "修改成功");
        } else {
            return new AjaxResult(HttpStatus.ERROR, "修改失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public AjaxResult deleteMapOrPoints(Long[] ids) {
        int length = 0;
        for (Long id : ids) {
            int delete = platformMMapMapper.deleteById(id);
            if(delete > 0) {
                length++;
            }
        }
        if (length == ids.length) {
            return new AjaxResult(HttpStatus.SUCCESS, "删除成功");
        }
        throw new RuntimeException("删除失败");
    }
}
